import time
import pyautogui
import pyperclip
import win32gui
import win32con

def activate_wechat_window():
    # 查找微信窗口
    hwnd = win32gui.FindWindow("WeChatMainWndForPC", None)
    if hwnd:
        # 激活窗口
        win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
        win32gui.SetForegroundWindow(hwnd)
        time.sleep(1)
        return True
    return False

def send_wechat_message(contact, message, interval=5, repeat=1):
    for _ in range(repeat):
        if activate_wechat_window():
            try:
                # 打开搜索框（快捷键Ctrl+Alt+W）
                # pyautogui.hotkey('ctrl', 'alt', 'w')
                # time.sleep(0.5)
                
                # 输入联系人名称
                # pyperclip.copy(contact)
                pyautogui.hotkey('ctrl', 'v')
                time.sleep(0.2)
                
                # 回车进入聊天
                pyautogui.press('enter')
                time.sleep(0.2)
                
                # 输入消息内容
                pyperclip.copy(message)
                pyautogui.hotkey('ctrl', 'v')
                time.sleep(0.2)
                
                # 发送消息
                pyautogui.press('enter')
                print(f"消息已发送至 {contact}")
                
                # 等待间隔
                time.sleep(interval)
                
            except Exception as e:
                print(f"发送失败：{str(e)}")
        else:
            print("未找到微信窗口，请确保微信已启动")

if __name__ == "__main__":
    # 配置参数
    contact_name = "文件传输助手"  # 必须与微信备注完全一致
    message_text = "是这样的"
    send_interval = 0.1        # 发送间隔（秒）
    repeat_times = 20             # 重复发送次数

    send_wechat_message(contact_name, message_text, send_interval, repeat_times)
    
